<?php

class CategoryController extends BaseAdminController {

    public $menu = array(
        array('label' => 'Create new category', 'url' => array('category/create')),
        array('label' => 'List all categories', 'url' => array('category/list'))
    );

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', // allow admin user to perform all actions
                'actions' => array('index', 'list', 'create', 'update', 'delete', 'search'),
                'users' => array('admin'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    /**
     * Get all models.
     */
    public function actionIndex() {
        $this->redirect(array('list'));
    }

    /**
     * 
     */
    public function actionSearch() {
        $categorySearch = new Search;
        if (isset($_GET['Search'])) {
            $categorySearch->attributes = $_GET['Search'];
            $keyWord = $categorySearch->keyWord;
            $criteria = new CDbCriteria;
            $criteria->distinct = true;
            $criteria->condition = "name_en like :keyWord and parent_id is not null";
            if (strlen($keyWord) > 0) {
                $criteria->params = array(':keyWord' => '%' . $keyWord . '%');
            }
            $dataProvider = new CActiveDataProvider('Category', array(
                'criteria' => $criteria,
                'pagination' => array(
                    'pageSize' => 15,
                ),
            ));
            $categorySearch = new Search;
            $this->render('list', array(
                'categorySearch' => $categorySearch,
                'dataProvider' => $dataProvider,
            ));
        }
    }

    /**
     * Manages all models.
     */
    public function actionList() {
        $categorySearch = new Search;
        $criteria = new CDbCriteria;
        //$criteria->condition = "parent_id is not null";
        $dataProvider = new CActiveDataProvider('Category', array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 15,
            ),
        ));

        $this->render('list', array(
            'categorySearch' => $categorySearch,
            'dataProvider' => $dataProvider,
        ));
    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'list' page.
     */
    public function actionCreate() {
        try {
            $category = new Category;
            $message = '';

            if (isset($_POST['Category'])) {
                // collects user input data
                $category->attributes = $_POST['Category'];
                if ($category->validate()) {
                    if ($category->save()) {
                        Yii::app()->user->setFlash('success', Yii::t('category', 'successCreate'));
                        $this->refresh();
                        $this->redirect(array('create'));
                    } else {
                        $message = Yii::t('category', Yii::t('category', 'failCreate'));
                    }
                } else {
                    $message = Yii::t('category', 'notValid');
                }
            }
            // displays the login form
            $this->render('create', array(
                'category' => $category,
                'message' => $message,
            ));
        } catch (Exception $ex) {
            throw new CHttpException(Yii::t('default', 'notExistWeb'));
        }
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'list' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id) {
        try {
            $category = $this->loadModel($id);
            $message = '';

            if (isset($_POST['Category'])) {
                // collects user input data
                $category->attributes = $_POST['Category'];
                if ($category->validate()) {
                    if ($category->save()) {
                        Yii::app()->user->setFlash('success', Yii::t('category', 'successUpdate'));
                        $this->redirect(array('list'));
                    } else {
                        $message = Yii::t('category', Yii::t('category', 'failUpdate'));
                    }
                } else {
                    $message = Yii::t('category', 'notValid');
                }
            }
            $this->render('update', array(
                'category' => $category,
                'message' => $message,
            ));
        } catch (Exception $ex) {
            throw new CHttpException(Yii::t('default', 'notExistWeb'));
        }
    }

    /**
     * D 


      eletes a   particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id) {
        if (Yii::app()->request->isPostRequest) {
// we only allow deletion via POST request
            $category = $this->loadModel($id);
            $category->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('list'));
        }
        else
            throw new CHttpException(400, Yii::t('default', 'notExistWeb'));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id) {
        $category = Category::model()->findByPk($id);
        if ($category === null)
            throw new CHttpException(404, Yii::t('default', 'notExistWeb'));
        return $category;
    }

}